%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%THIS FILE ESTIMATES 200 BOOTSTRAP REPLICATIONS OF THE ORDERED PROBIT MODEL IN 
%"Evidence of Neighborhood Effects from Moving to Opportunity: 
%     LATEs of Neighborhood Quality"
%by Dionissi Aliprantis and Francisca G.-C. Richter
%SEE THE INCLUDED readme.txt FILE FOR AN EXPLANATION OF THIS FILE 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

options = optimset('TolFun', 1e-4, 'TolX', 1e-4, 'GradObj', 'off', 'MaxIter', 500000, 'MaxFunEvals', 500000); 

x0=zeros(63,1);

%BETAS
x0(1)=0.09;
x0(2)=0.12;
x0(3)=0.04;
x0(4)=0.12;
x0(5)=0.40;
x0(6)=0.15;
x0(7)=-0.43;
x0(8)=-0.51;

%CUTPOINTS
x0(9)=-0.95;
x0(10)=-0.45;
x0(11)=-0.07;
x0(12)=0.30;
x0(13)=0.60;
x0(14)=0.98;
x0(15)=1.31;
x0(16)=1.66;
x0(17)=2.30;
      
%gamma8_j
x0(18)=0.75;
x0(19)=0.75;
x0(20)=0.75;
x0(21)=0.55;
x0(22)=0.55;
x0(23)=0.55;
x0(24)=0.50;
x0(25)=0.40;
x0(26)=0.40;

x0(27)=0.0;
x0(28)=-0.18;
x0(29)=0.27;
x0(30)=0.26;

%gamma1_j
x0(31)=1.00;
x0(32)=1.00;
x0(33)=1.00;
x0(34)=0.80;
x0(35)=0.80;
x0(36)=0.80;
x0(37)=0.75;
x0(38)=0.65;
x0(39)=0.65;

x0(40)=-0.12;
x0(41)=-0.15;
x0(42)=0.24;
x0(43)=0.14;

%%%%%%%%%%
%SECTION 8 TYPES from probit  svy_cmove $X [pw= wt_totsvy] if exp==1
%%%%%%%%%%
x0(44)=0.53;
x0(45)=0.15;
x0(46)=0.07;
x0(47)=-0.03;
x0(48)=-0.43;
x0(49)=-0.07;
x0(50)=0.38;
x0(51)=-0.60;

x0(52)=0.07;

% x0(44)=0.1;
% x0(45)=0.1;
% x0(46)=0.1;
% x0(47)=0.1;
% x0(48)=0.1;
% x0(49)=0.1;
% x0(50)=0.1;
% x0(51)=0.1;
% 
% x0(52)=0.1;


%%%%%%%%%%
%EXP TYPES from probit  svy_cmove $X [pw= wt_totsvy] if exp==2
%%%%%%%%%%
x0(53)=0.41;
x0(54)=0.02;
x0(55)=0.12;
x0(56)=-0.08;
x0(57)=0.00;
x0(58)=-0.56;
x0(59)=0.36;
x0(60)=-0.15;

x0(61)=-0.17;

% x0(53)=0.1;
% x0(54)=0.1;
% x0(55)=0.1;
% x0(56)=0.1;
% x0(57)=0.1;
% x0(58)=0.1;
% x0(59)=0.1;
% x0(60)=0.1;
% 
% x0(61)=0.1;

       
%RHO's
x0(62)=0.3;
x0(63)=-0.3;





save 'x_BS_1_200.mat';









%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%STANDARD ERRORS - BOOTSTRAPPING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
data=importdata('F:\matlab\matlab_data.txt');
N=length(data(:,13));

x_BS=zeros(200,63);
x_BS_1_200=zeros(200,63);

load x_BS_1_200.mat;
 for iter=1:200
     tic
     rng(2348+iter);
     data_BS=datasample(data,N);
     data_BS=data_BS(:,1:13);
     data_BS( any(data_BS==-9,2), : ) = [];
     [x,fval,exitflag] = fminsearch(@(x) LL_MTO2_BS(x,data_BS), x0, options);
     x_BS(iter,:)=x(:);
     x_BS_1_200=x_BS;
     save 'x_BS_1_200.mat';
     disp(iter)
     disp(toc/3600)
 end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CLEANING THE OUTPUT 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
load 'x_BS_1_200.mat';
clearvars -except x_BS_1_200
save 'x_BS_1_200.mat';

clear
x_BS=zeros(200,63);
load 'x_BS_1_200.mat';
clearvars -except x_BS x_BS_1_200
for iter=1:200
  x_BS(iter,:)= x_BS_1_200(iter,:);  
end
save 'x_BS.mat';


